package xxy.com.customview.view;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import xxy.com.customview.R;

/**
 * Created by Administrator on 2018/1/12.
 */

public class MyLineView extends View {

    private Path path;
    private Paint paint;
    private Paint paint1;
    private float[] points;
    private Path path_triangle;
    private Paint paint3;
    private Paint paint4;
    private Path path2;
    private Paint paint5;
    private Paint paint6;
    private Path path3;

    public MyLineView(Context context) {
        super(context);
        initView();
    }

    public MyLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initView() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(5);
        paint.setPathEffect(new CornerPathEffect(50));   //设置路径效果
        paint.setTextSize(80);
        paint1 = new Paint();
        paint1.setColor(Color.RED);
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setStrokeWidth(10);
        paint3 = new Paint();
        paint3.setColor(Color.GREEN);
        paint3.setStyle(Paint.Style.FILL);
        paint4 = new Paint();
        paint4.setStyle(Paint.Style.STROKE);
        paint4.setStrokeWidth(15);
        paint4.setColor(Color.WHITE);
        paint4.setPathEffect(new DashPathEffect(new float[]{20, 20}, 10));
        paint5 = new Paint();
        //给画笔设置调色盘中的一种
        paint5.setShader(new BitmapShader(BitmapFactory.decodeResource(getResources(), R.drawable.eight), Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
        paint6 = new Paint();


        path = new Path();
        path.lineTo(100, 200);
        path.moveTo(100, 200);   //把起始点移到（x,y),不改变坐标系的位置
        path.lineTo(200, 0);
        path.arcTo(100, 200, 600, 600, -90, 180, false);   //前四个点确定包裹着圆弧的矩形大小，后两个角度代表从哪个角开始，要旋转多少度，最后一个表示是否要连着前面已画过的最后一个轮廓

        //红色的矩形
        points = new float[]{100, 200, 600, 200, 600, 200, 600, 600, 600, 600, 100, 600, 100, 600, 100, 200};   //每四个点为一组，代表一条直线的起始点和结束点

        //三角形
        path_triangle = new Path();
        path_triangle.moveTo(100, 700);
        //线是连起来画的
        path_triangle.lineTo(600, 700);
        path_triangle.lineTo(350, 1000);
//        path_triangle.close();
        //爱心
        path2 = new Path();
        path2.addArc(700, 200, 900, 400, -225, 225);
        path2.arcTo(900, 200, 1100, 400, -180, 225, false);
        path2.lineTo(900, 542);

        path3 = new Path();
        path3.moveTo(900,600);
        path3.lineTo(900,1300);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(path, paint);
        canvas.drawPoint(600, 600, paint1);
        canvas.drawLines(points, paint1);
        canvas.drawPath(path_triangle, paint3);
        canvas.drawRoundRect(100, 1200, 600, 1400, 15, 15, paint3);   //
        canvas.drawPath(path3, paint);
        canvas.drawTextOnPath("喜欢曾经的NBA", path3, 0, 0, paint);
        canvas.drawColor(Color.parseColor("#8888ffff"));   //半透明蒙版
        canvas.drawCircle(350, 1300, 200, paint4);
        canvas.drawCircle(350, 1300, 200, paint5);
        //圆弧和扇形
        canvas.drawArc(100, 1600, 600, 1800, -120, 80, true, paint3);
        canvas.drawArc(100, 1600, 600, 1800, 20, 120, false, paint3);
        canvas.drawArc(100, 1600, 600, 1800, 150, 80, false, paint);
        //爱心
        canvas.drawPath(path2, paint3);

    }
}
